package YAPserver.logic;

import java.util.Observable;
import java.util.Observer;

import YAPclient.sound.SoundList.SoundName;
import YAPserver.GameActualizer;
import YAPserver.gameObjects.AbstractBall;
import YAPserver.logic.GameState;
import YAPserver.score.EndScore;
/**
 * This class handles all the output that comes from the game logic, (mainly from GamePlay.)
 * The output (not the object) is in the form of strings which are sent to the gameActualizer which then sends that data
 * to the clients.
 * @author Daniel
 *
 */
public class Output implements Observer{
	
	private String m_fieldCorners;
	private String m_objectPos;
	private String m_playerStat;
	private String m_highScore;
	private GameState m_gameState;
	private GameActualizer m_actual;
	
	/**
	 * Sole constructor
	 */
	public Output(){
		
	}
	/**
	 * Links the gameState, which keeps track of all the objects positions, to this output.
	 * @param gameState gameState to be linked
	 */
	public void setGameState(GameState gameState){
		this.m_gameState = gameState;
		this.m_gameState.addObserver(this);
	}
	
	@Override
	public void update(Observable obs, Object message) {
		if(obs instanceof GameState){
			if(message.equals("objectPos")){
				m_objectPos = ((GameState) obs).getObjectPos();
				return;
			}
//			if(message.equals("playerStat")){
//				m_playerStat = ((GameState) obs).getPlayerStat();
//				m_actual.sendPlayerStat(m_playerStat);
//				return;
//			}
		}
		if(obs instanceof Field){
			m_fieldCorners = (String) message;
			return;
		}
		if(obs instanceof EndScore){
			m_highScore = (String) message;
			return;
		}
		if(obs instanceof Player){
			m_playerStat = ((Player) obs).toString();
			m_actual.sendPlayerStat(m_playerStat, ((Player) obs).getId());
		}
	}
	
	/**
	 * Links the gameActualizer, which sends data packets to the client, with this output.
	 * @param actual
	 */
	public void linkActualiser(GameActualizer actual){
		m_actual = actual;
	}
	
	/**
	 * Sends the message that the clients need to rotate.
	 * @param s string message with the information for letting a client rotate
	 */
	public void sendRotation(String s) {
		m_actual.sendRotation(s);
	}
	
	/**
	 * Returns the corners of the generated field
	 * @return string of corners
	 * @see Field
	 */
	public String getFieldCorners(){
		return m_fieldCorners;
	}
	
	/**
	 * Returns the positions of all the gameObjects in the field in form of a string.
	 * @return the positions of all the gameObjects in the field
	 * @see GameState 
	 */
	public String getObjectPos(){
		return m_objectPos;
	}
	
	/**
	 * Returns the number of lives a player has.
	 * @return the number of lives a player has
	 */
	public String getPlayerStat() {
		return m_playerStat;
	}
	
	/**
	 * Returns the end score of the game once it has ended.
	 * @return the end score
	 */
	public String getHighScore(){
		return m_highScore;
	}
	
	/**
	 * Sends the message to the client that it should make a bounce sound.
	 */
	public void sendBounce(AbstractBall ball) {
		if(ball.getRadius() > 15){
			m_actual.sendSound(SoundName.bounceBig.ordinal());
			return;
		}
		if(ball.getRadius() < 4){
			m_actual.sendSound(SoundName.bounceSmall.ordinal());
			return;
		}
		m_actual.sendSound(SoundName.bounce.ordinal());
	}
	
	/**
	 * Sends the message to the client that it should make the sound that a mod is activated.
	 */
	public void sendModActivated() {
		// TODO Auto-generated method stub
		
	}
}
